//
//  JWTDrawShapedView.m
//  JWT
//
//  Created by ndm on 17/2/5.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTDrawShapedView.h"
#define ImageView_Width 34

@implementation JWTDrawShapedView

//左上
-(instancetype)initWithFrame:(CGRect)frame andLeftTop:(CGFloat) margin andBackColor:(UIColor *)color
                andImageName:(NSString *) imageName andLabelText:(NSString *)text{
    self = [super initWithFrame:frame];
    if (self) {
        CGSize finalSize = CGSizeMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        CGFloat layerHeight = finalSize.height;
        CAShapeLayer *layer = [CAShapeLayer layer];
        UIBezierPath *bezier = [UIBezierPath bezierPath];
        [bezier moveToPoint:CGPointMake(0, finalSize.height - layerHeight)];
        [bezier addLineToPoint:CGPointMake(0, finalSize.height-1)];
        [bezier addLineToPoint:CGPointMake(finalSize.width-margin, finalSize.height - 1)];
        [bezier addLineToPoint:CGPointMake(finalSize.width, finalSize.height - layerHeight)];
        [bezier addLineToPoint:CGPointMake(0,0)];
        layer.path = bezier.CGPath;
        layer.fillColor = color.CGColor;
        [self.layer addSublayer:layer];
        
        UIImageView *courseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(UISCREEN_WIDTH*0.5*0.5-ImageView_Width*0.5, (self.height-ImageView_Width-34-Cell_Margin)*0.5-ImageView_Width*0.5, ImageView_Width, ImageView_Width*1.5)];
        [courseImageView setImage:[UIImage imageNamed:imageName]];
        [self addSubview:courseImageView];
        //        courseImageView.center = CGPointMake(courseLabel.center.x,courseImageView.center.y );
        
        UILabel *courseLabel = [[UILabel alloc] initWithFrame:CGRectMake(Cell_Margin,courseImageView.bottom+Cell_Margin, UISCREEN_WIDTH*0.5-2*Cell_Margin, 34)];
        courseLabel.text = text;
        courseLabel.textColor = RGB_WhiteBackGround;
        courseLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:courseLabel];
        
        
    }
    return self;
}
//右上
-(instancetype)initWithFrame:(CGRect)frame andRightTop:(CGFloat) margin andBackColor:(UIColor *)color
                andImageName:(NSString *) imageName andLabelText:(NSString *)text{
    self = [super initWithFrame:frame];
    if (self) {
        CGPoint finalPoint = CGPointMake(self.frame.origin.x, self.frame.origin.y);
        CGSize finalSize = CGSizeMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        CGFloat layerHeight = finalSize.height;
        CAShapeLayer *layer = [CAShapeLayer layer];
        UIBezierPath *bezier = [UIBezierPath bezierPath];
        [bezier moveToPoint:CGPointMake(0, finalSize.height - layerHeight)];//左上坐标
        [bezier addLineToPoint:CGPointMake(-margin, finalSize.height-1)];//左下坐标
        [bezier addLineToPoint:CGPointMake(finalSize.width+margin, finalSize.height - 1)];//下平行线 右下坐标
        [bezier addLineToPoint:CGPointMake(finalPoint.x+finalSize.width, finalSize.height - layerHeight)];//上平行线 右上坐标
        [bezier addLineToPoint:CGPointMake(0,0)];
        layer.path = bezier.CGPath;
        layer.fillColor = color.CGColor;
        [self.layer addSublayer:layer];
        
        UIImageView *courseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(UISCREEN_WIDTH*0.4*0.5-ImageView_Width*0.75, (self.height-ImageView_Width-34-Cell_Margin)*0.5, ImageView_Width*1.5, ImageView_Width)];
        [courseImageView setImage:[UIImage imageNamed:imageName]];
        [self addSubview:courseImageView];
        
        UILabel *courseLabel = [[UILabel alloc] initWithFrame:CGRectMake(Cell_Margin, courseImageView.bottom+Cell_Margin, UISCREEN_WIDTH*0.4-2*Cell_Margin, 34)];
        courseLabel.text = text;
        courseLabel.textColor = RGB_WhiteBackGround;
        courseLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:courseLabel];
    }
    return self;
}

//左下
-(instancetype)initWithFrame:(CGRect)frame andLeftBottom:(CGFloat) margin andBackColor:(UIColor *)color andImageName:(NSString *) imageName andLabelText:(NSString *)text{
    self = [super initWithFrame:frame];
    if (self) {
        CGSize finalSize = CGSizeMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        CGFloat layerHeight = finalSize.height;
        CAShapeLayer *layer = [CAShapeLayer layer];
        UIBezierPath *bezier = [UIBezierPath bezierPath];
        [bezier moveToPoint:CGPointMake(0, finalSize.height - layerHeight)];//左上坐标
        [bezier addLineToPoint:CGPointMake(0, finalSize.height-1)];//左下坐标
        [bezier addLineToPoint:CGPointMake(finalSize.width-margin, finalSize.height - 1)];//下平行线 右下坐标
        [bezier addLineToPoint:CGPointMake(finalSize.width, finalSize.height - layerHeight)];//上平行线 右上坐标
        [bezier addLineToPoint:CGPointMake(0,0)];
        layer.path = bezier.CGPath;
        layer.fillColor = color.CGColor;
        [self.layer addSublayer:layer];
        
        UIImageView *courseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(UISCREEN_WIDTH*0.4*0.5-ImageView_Width*0.5, (self.height-ImageView_Width-34-Cell_Margin)*0.5, ImageView_Width, ImageView_Width)];
        [courseImageView setImage:[UIImage imageNamed:imageName]];
        [self addSubview:courseImageView];
        
        UILabel *courseLabel = [[UILabel alloc] initWithFrame:CGRectMake(Cell_Margin, courseImageView.bottom+Cell_Margin, UISCREEN_WIDTH*0.4-2*Cell_Margin, 34)];
        courseLabel.text = text;
        courseLabel.textColor = RGB_WhiteBackGround;
        courseLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:courseLabel];
    }
    return self;
}
//右下
-(instancetype)initWithFrame:(CGRect)frame andRightBottom:(CGFloat) margin andBackColor:(UIColor *)color andImageName:(NSString *) imageName andLabelText:(NSString *)text{
    self = [super initWithFrame:frame];
    if (self) {
        CGSize finalSize = CGSizeMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        CGFloat layerHeight = finalSize.height;
        CAShapeLayer *layer = [CAShapeLayer layer];
        UIBezierPath *bezier = [UIBezierPath bezierPath];
        [bezier moveToPoint:CGPointMake(0, finalSize.height - layerHeight)];//左上坐标
        [bezier addLineToPoint:CGPointMake(-margin, finalSize.height-1)];//左下坐标
        [bezier addLineToPoint:CGPointMake(finalSize.width, finalSize.height - 1)];//下平行线 右下坐标
        [bezier addLineToPoint:CGPointMake(finalSize.width, finalSize.height - layerHeight)];//上平行线 右上坐标
        [bezier addLineToPoint:CGPointMake(0,0)];
        layer.path = bezier.CGPath;
        layer.fillColor = color.CGColor;
        [self.layer addSublayer:layer];
        
        UIImageView *courseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(UISCREEN_WIDTH*0.5*0.5-ImageView_Width*0.75, (self.height-ImageView_Width-34-Cell_Margin)*0.5, ImageView_Width*1.5, ImageView_Width)];
        [courseImageView setImage:[UIImage imageNamed:imageName]];
        [self addSubview:courseImageView];
//        courseImageView.center = CGPointMake(courseLabel.center.x,courseImageView.center.y );
        
        UILabel *courseLabel = [[UILabel alloc] initWithFrame:CGRectMake(Cell_Margin,courseImageView.bottom+Cell_Margin, UISCREEN_WIDTH*0.5-2*Cell_Margin, 34)];
        courseLabel.text = text;
        courseLabel.textColor = RGB_WhiteBackGround;
        courseLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:courseLabel];
        
        
    }
    return self;
}

@end
